/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


#include "spellcheckermanager.h"

#include "paths.h"
#include "dummyspellchecker.h"
#include "log.h"
#ifdef HUNSPELL_FOUND
#include "hunspell_spellchecker.h"
#endif

namespace Wishmaster
{
	SpellCheckerManager::SpellCheckerManager() : Singleton<SpellCheckerManager>()
	{
#ifdef HUNSPELL_FOUND
		SpellCheckerPtr sp(new HunspellSpellChecker());
		// TODO search all dictionaries and add it
		// TODO request config for dictionaries
        log(llDebug, "Searching for hunspell dictionaries");
        bool russian = sp->addDictionary(DefaultPath::hunspellDictionaries() + QDir::separator() + "ru_RU");
        bool english = sp->addDictionary(DefaultPath::hunspellDictionaries() + QDir::separator() + "en_US");
		if(russian || english)
		{
			m_spellcheckers.insert(sp->id(), sp);
			m_default = sp;
		}
#endif
		if(m_default.isNull())
		{
			SpellCheckerPtr sp(new DummySpellChecker());
			m_spellcheckers.insert(sp->id(), sp);
			m_default = sp;
		}
	}

	SpellCheckerManager::~SpellCheckerManager()
	{
	}

	QStringList SpellCheckerManager::listSpellcheckers()
	{
		QStringList list;
		for(QMap<QString, SpellCheckerPtr>::iterator it = m_spellcheckers.begin();
				it != m_spellcheckers.end(); ++it)
		{
			list.append(it.key());
		}
		return list;
	}

	SpellCheckerPtr SpellCheckerManager::spellchecker(const QString& id)
	{
		return m_spellcheckers.value(id);
	}

	SpellCheckerPtr SpellCheckerManager::defaultSpellChecker()
	{
		return m_default;
	}
}

